package com.chainyoung.trademarket.mvp.presenter;

import com.chainyoung.common.appcommon.entity.HdWalletCache;
import com.chainyoung.common.appcommon.entity.Version;
import com.chainyoung.common.base.CommonSubscriber;
import com.chainyoung.common.bean.UserInfo;
import com.chainyoung.common.constant.LibConstant;
import com.chainyoung.common.di.scope.ActivityScope;
import com.chainyoung.common.exception.ApiException;
import com.chainyoung.common.mvp.BasePresenter;
import com.chainyoung.common.utils.RxUtils;
import com.chainyoung.component_tmperson.entity.MyTotalAsset;
import com.chainyoung.trademarket.entity.Ad;
import com.chainyoung.trademarket.mvp.contract.MainContract;

import java.util.List;
import java.util.TreeMap;

import javax.inject.Inject;

import static com.chainyoung.common.constant.LibConstant.NO_RESULT_ERRORCODE;

@ActivityScope
public class MainPresenter extends BasePresenter<MainContract.Model, MainContract.View> {

    @Inject
    public MainPresenter(MainContract.Model model, MainContract.View view) {
        super(model, view);
    }


    public void getUserInfo(TreeMap<String, Object> map) {
        addSubscribe(mModel.getUserInfo(map)
                .compose(RxUtils.<UserInfo>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<UserInfo>(mView,true) {
                    @Override
                    public void onNext(UserInfo object) {
                        mView.onGetUserInfoSuccess(object);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                }));
    }


//    public void getIndexAds(TreeMap<String, Object> map) {
//        addSubscribe(mModel.getIndexAds(map)
//                .compose(RxUtils.<List<Ad>>rxSchedulerHelper())
//                .subscribeWith(new CommonSubscriber<List<Ad>>(mView) {
//                    @Override
//                    public void onNext(List<Ad> object) {
//                        mView.onGetIndexAdSuccess(object);
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        if (e instanceof ApiException) {
//                            if (((ApiException) e).getErrorCode().endsWith(LibConstant.NO_RESULT_ERRORCODE)) {
//                                mView.onGetIndexAdSuccess(null);
//                                return;
//                            }
//                        }
//                        super.onError(e);
//                    }
//                }));
//    }

//    public void getTotalAsset(TreeMap<String, Object> map) {
//        addSubscribe(mModel.getTotalAsset(map)
//                .compose(RxUtils.<MyTotalAsset>rxSchedulerHelper())
//                .subscribeWith(new CommonSubscriber<MyTotalAsset>(mView) {
//                    @Override
//                    public void onNext(MyTotalAsset object) {
//                        mView.onGetTotalAsset(object);
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        super.onError(e);
//                    }
//                }));
//    }

    public void checkUpdate() {
        addSubscribe(mModel.checkUpdate()
                .compose(RxUtils.<Version>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<Version>(mView) {
                    @Override
                    public void onNext(Version data) {
                        mView.showVersion(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e instanceof ApiException) {
                            if (((ApiException) e).getErrorCode().equals(NO_RESULT_ERRORCODE)) {
                                mView.showVersion(null);
                                return;
                            }
                        }
                        super.onError(e);
                    }
                }));
    }

    public void addHdWalletCache(HdWalletCache item) {
        addSubscribe(mModel.addHdWalletCache(item)
                .compose(RxUtils.<HdWalletCache>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<HdWalletCache>(mView,true) {
                    @Override
                    public void onNext(HdWalletCache data) {
                        mView.onAddHdWalletCacheSuccess(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                }));
    }

    public void getAllHdWalletCaches(String coinType) {
        addSubscribe(mModel.getAllHdWalletCaches(coinType)
                .compose(RxUtils.<List<HdWalletCache>>rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<List<HdWalletCache>>(mView) {
                    @Override
                    public void onNext(List<HdWalletCache> data) {
                        mView.onGetAllHdWalletCaches(data);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                }));


    }







}
